package org.desktop.web.client.gui.models;

import org.desktop.web.client.role.proxy.RpcRolesProxy;
import org.desktop.web.shared.domain.entities.Role;

import com.google.gwt.user.client.rpc.IsSerializable;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.data.client.loader.RpcProxy;
import com.sencha.gxt.data.shared.LabelProvider;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.data.shared.loader.FilterPagingLoadConfig;
import com.sencha.gxt.data.shared.loader.FilterPagingLoadConfigBean;
import com.sencha.gxt.data.shared.loader.LoadResultListStoreBinding;
import com.sencha.gxt.data.shared.loader.PagingLoadResult;
import com.sencha.gxt.data.shared.loader.PagingLoader;
import com.sencha.gxt.widget.core.client.form.ComboBox;

public class PagingComboBox<M extends IsSerializable, Rpc extends RpcProxy<FilterPagingLoadConfig, PagingLoadResult<M>>, S extends ListStore<M>>
		implements IsWidget {

	private ComboBox<M> combo;

	private PagingLoader<FilterPagingLoadConfig, PagingLoadResult<M>> loader;

	private boolean allowBlank = true;
	private String name = "no-name";

	private boolean typeAhead = false;

	private String emptyText = "Select....";

	private int pageSize = 20;

	private int minChars = 3;

	public PagingComboBox(Rpc proxy, S store, LabelProvider<M> textVisible) {
		// ListStore<Role> roleListStore = new
		// ListStore<Role>(roleProperties.codRole());
		// RpcRolesProxy proxy = new RpcRolesProxy();
		loader = new PagingLoader<FilterPagingLoadConfig, PagingLoadResult<M>>(
				proxy);
		loader.setRemoteSort(true);
		loader.addLoadHandler(new LoadResultListStoreBinding<FilterPagingLoadConfig, M, PagingLoadResult<M>>(
				store));

		this.combo = new ComboBox<M>(store, textVisible);
		this.combo.setLoader(loader);
		this.setEmptyText(this.emptyText);

		this.setName(this.name);
		
		loader.useLoadConfig(new FilterPagingLoadConfigBean());
	}

	@Override
	public Widget asWidget() {
		// TODO Auto-generated method stub
		return combo;
	}

	public ComboBox<M> getCombo() {
		return combo;
	}

	public void setCombo(ComboBox<M> combo) {
		this.combo = combo;
	}

	public boolean isAllowBlank() {
		return allowBlank;
	}

	public void setAllowBlank(boolean allowBlank) {
		this.allowBlank = allowBlank;
		this.combo.setAllowBlank(allowBlank);

	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
		this.combo.setName(name);
	}

	public boolean isTypeAhead() {
		return typeAhead;
	}

	public void setTypeAhead(boolean typeAhead) {
		this.typeAhead = typeAhead;
		this.combo.setTypeAhead(typeAhead);
	}

	public String getEmptyText() {
		return emptyText;
	}

	public void setEmptyText(String emptyText) {
		this.emptyText = emptyText;
		this.combo.setEmptyText(emptyText);
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
		this.combo.setPageSize(pageSize);
	}

	public int getMinChars() {
		return minChars;
	}

	public void setMinChars(int minChars) {
		this.minChars = minChars;
		this.combo.setMinChars(minChars);
	}

	public PagingLoader<FilterPagingLoadConfig, PagingLoadResult<M>> getLoader() {
		return loader;
	}

}
